package com.emar.spring.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emar.spring.common.util.StringUtil;
import com.emar.spring.dto.ChartDTO;
import com.emar.spring.dto.ParamsDTO;
import com.emar.spring.dto.SingleDataDTO;
import com.emar.spring.service.RecodeService;

/**
 * 实际记录操作类 
 */
@Controller
@RequestMapping(value="/recode")
public class RecodeController {

	@Autowired
	private RecodeService recodeService;
	
	/**
	 * 获取记录表数据，内封装了查询条件
	 * @param page
	 * @param rows
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getrecodelist",method=RequestMethod.POST)
	@ResponseBody
	public String getProjectList(
			@ModelAttribute ParamsDTO pe,
			@RequestParam(value="page",defaultValue="0") int page,
			@RequestParam(value="rows",defaultValue="10") int rows,
			Model model,ServletRequest request){
//		pe.setSecurityLevelCode(securityLevelCode);
		
		int offset = (page-1)*rows;
		int total = recodeService.getCount(pe,null);
		List<ParamsDTO> proList = recodeService.getDtoPageList(pe, null, offset, rows);
		
		JSONArray arr = new JSONArray(proList);
		JSONObject data = new JSONObject();
		data.put( "total", total);
		data.put( "rows", arr);
		
		return data.toString();
	}
	
	/**
	 * 获取查询结果
	 * @param pe
	 * @param page
	 * @param rows
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/recordlist",method=RequestMethod.POST)
	@ResponseBody
	public String getProjectListGroup(
//			ServletRequest request,
			@ModelAttribute ParamsDTO pe,
			@RequestParam(value="page",defaultValue="0") int page,
			@RequestParam(value="rows",defaultValue="10") int rows,
			Model model
			){
		int offset = (page-1)*rows;
		int total = recodeService.getRecodeCountGroup(pe,pe.getGroupBy());
		List<ParamsDTO> proList = recodeService.getDtoPageList(pe, pe.getGroupBy(), offset, rows);
		
		JSONArray arr = new JSONArray(proList);
		JSONObject data = new JSONObject();
		data.put( "total", total);
		data.put( "rows", arr);
		
		return data.toString();
	}
	
	/**
	 * 获取数据，封装为json格式
	 * @param groupBy
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getpie",method=RequestMethod.POST)
	public String ownerGetByDayPie(
			@RequestParam(defaultValue="systemId") String groupBy,
			Model model,ServletRequest request){
		JSONObject json2 = this.getJson2(groupBy);	//调用下面封装的方法
		model.addAttribute( "json", json2);
		model.addAttribute( "url",  "recordpie");
		return "chartdemo/pie";
	}
	/**
	 * 获取数据，封装为json格式
	 * @param groupBy
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getequipmentpie",method=RequestMethod.POST)
	public String getequipmentpie(
			@ModelAttribute ParamsDTO pe,
			Model model,ServletRequest request){
		JSONObject json2 = this.getJson1(pe);	//调用下面封装的方法
		model.addAttribute( "json", json2);
		model.addAttribute( "url",  "equipmentpie");
		return "chartdemo/pie";
	}
	/**
	 * 封装json格式
	 * @param groupBy
	 * @return
	 */
	private JSONObject getJson1(ParamsDTO pe){
		ChartDTO chart = new ChartDTO( "符合率", "", "",  " (s)",  "", "0");
		JSONObject json = new JSONObject();
		
		pe.setIsValid( "是");				//适用
		
		List<ParamsDTO> list = recodeService.getProjectDTOCountList(pe,"RecordJudge");	//把“该系统下所有对象的所有测评指标的总和-不适用的问题总数”作为问题总量,即适用的问题量；
		if(list.size()>0 && list !=null){
			ParamsDTO d  = list.get(0);
			chart.setSubcaption(d.getObjType1());
		}
		
		List<SingleDataDTO> singleList = new ArrayList<SingleDataDTO>();
		for(ParamsDTO r :list){
			SingleDataDTO s = new SingleDataDTO();
			s.setLabel( r.getObjType1()+"-"+r.getRecordJudge());
			s.setValue( String.valueOf(r.getCount()));
			singleList.add(s);
		}
		
		JSONObject chart1 = new JSONObject(chart);
		json.put( "chart",  chart1);
		json.put( "data", singleList);
		return json;
	}
	
	/**
	 * 封装json格式
	 * @param groupBy
	 * @return
	 */
	private JSONObject getJson2(String groupBy){
		ChartDTO chart = new ChartDTO( "符合率", "", "",  " (s)",  "", "0");
		JSONObject json = new JSONObject();
		JSONObject chart1 = new JSONObject(chart);
		json.put( "chart",  chart1);
		ParamsDTO rd = new ParamsDTO();
		rd.setIsValid( "是");				//适用
		
		List<ParamsDTO> list = recodeService.getProjectDTOCountList(rd,groupBy);	//把“该系统下所有对象的所有测评指标的总和-不适用的问题总数”作为问题总量,即适用的问题量；
		List<SingleDataDTO> singleList = new ArrayList<SingleDataDTO>();
		for(ParamsDTO r :list){
			SingleDataDTO s = new SingleDataDTO();
			s.setLabel( r.getCommissionCorpName()+"-"+r.getSystemName());
			s.setValue( String.valueOf(r.getCount()));
			singleList.add(s);
		}
		
		json.put( "data", singleList);
		return json;
	}
}
